package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import com.zzyl.nursing.vo.MemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

import static com.zzyl.common.utils.PageUtils.startPage;

/**
 * 客户老人关联Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-18
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService
{
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询客户老人关联
     * 
     * @param id 客户老人关联主键
     * @return 客户老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询客户老人关联列表
     * 
     * @param familyMemberElder 客户老人关联
     * @return 客户老人关联
     */
    @Override
    public List<FamilyMemberElder> selectFamilyMemberElderList(FamilyMemberElder familyMemberElder)
    {
        return familyMemberElderMapper.selectFamilyMemberElderList(familyMemberElder);
    }

    /**
     * 新增客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    @Override
    public int insertFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return save(familyMemberElder) ? 1 : 0;
    }

    /**
     * 修改客户老人关联
     * 
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder)
    {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除客户老人关联
     * 
     * @param ids 需要删除的客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除客户老人关联信息
     * 
     * @param id 客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 绑定家人
     *
     * @param familyMemberElderDto 家人信息
     * @return  0 失败 1 成功
     */
    @Override
    public int bindingFamilyMember(FamilyMemberElderDto familyMemberElderDto) {
        FamilyMemberElder familyMemberElder = new FamilyMemberElder();

        // 1.从ThreadLocal中获取当前登录人的id
        Long familyMemberId = UserThreadLocal.getUserId();
        familyMemberElder.setFamilyMemberId(familyMemberId);

        // 2.根据老人姓名和老人身份证号查询老人id
        Elder elder = elderMapper.selectOne(Wrappers.<Elder>lambdaQuery()
                .eq(Elder::getName, familyMemberElderDto.getName())
                .eq(Elder::getIdCardNo, familyMemberElderDto.getIdCard()));

        if (ObjectUtil.isEmpty(elder)) {
            return 0;
        }
        familyMemberElder.setElderId(elder.getId());

        // 3.设置称呼
        familyMemberElder.setRemark(familyMemberElderDto.getRemark());

        // 4.保存结果并返回
        return familyMemberElderMapper.insert(familyMemberElder);
    }

    /**
     * 查询家人列表
     *
     * @return 家人列表
     */
    @Override
    public List<FamilyMemberElderVo> getMyFamilyMember() {
        Long userId = UserThreadLocal.getUserId();
        return familyMemberElderMapper.getMyFamilyMember(userId);
    }

    /**
     * 分页查询已绑定家人列表
     *
     * @return 已绑定家人列表
     */
    @Override
    public List<MemberElderVo> listByPage() {
        startPage();
        Long memberId = UserThreadLocal.get();
        return familyMemberElderMapper.listByPage(memberId);
    }
}
